class RoomsController < ApplicationController

  before_filter :query_statistics,:only=>[:index,:pre_allocation,:allocation]
  include ApplicationHelper

  def index
    @rooms = @event.rooms.paginate :page => params[:page], :per_page => 10
    
  end
  
  def create
    @room = Room.new(params[:room])
    event = Event.find(params[:event])
    @room.event = event
    @room.save
    render :update do |page|
      page.create(@room)
    end
  end

  def delete
    room = Room.find(params[:id])
    del(room)
  end

  def query
    if params[:id]
      @rooms = Room.find_all_by_room_category(params[:id])
    else
      @rooms=Room.all
    end
    render :partial=>'room_list',:locals=>{:rooms=>@rooms}
  end

  def pre_allocation
    @start_index=Level::OPTIONS.index(params[:room][:start_level])
    @end_index=Level::OPTIONS.index(params[:room][:end_level])
    @room_category=params[:room_category]
    @count = params[:room][:count]
    @users=get_users_by_level(@event,@start_index,@end_index)
    @contain_users_count=(@room_hash[@room_category]["allocation"].size +  @room_hash[@room_category]["unallocation"].size) * @count.to_i
  end

  #分配房间
  def allocation
    event_id=params[:event_id]
    start_index = params[:start_index].to_i
    end_index=params[:end_index].to_i
    room_category = params[:room_category]
    count = params[:count].to_i
    users=get_users_by_level(@event,start_index,end_index)
    rooms=@room_hash[room_category]["allocation"] +  @room_hash[room_category]["unallocation"]
    rooms.each do |room|
#      room.users.clear
      count.times{
        user = users.shift
        if user
#          room.users << user
          conventioner = user.conventioners.find_by_event_id(event_id)
          conventioner.room = room;
          conventioner.save
#          users.delete(user)
        end
      }
    end
    redirect_to event_rooms_path(:event_id=>event_id)
  end

  def cancel_allocation
    render :text=>''
  end

  def remove_user
    arr = params[:id].split(',')
    room = Room.find(arr[0].to_i)
    user = User.find(arr[1].to_i)
    conventioner =user.conventioners.find_by_event_id(room.event_id)
    conventioner.room=nil
    if conventioner.save
#    if room.users.delete(user)
      render :update do |page|
        page.remove("room_#{room.id}_user_#{user.id}")
        page.replace_html 'unchecked_in',conventioner.event.unchecked_in.size
        page['unchecked_in'].visual_effect :highlight
        page.replace_html 'checked_in',conventioner.event.checked_in.size
        page['checked_in'].visual_effect :highlight
      end
    end
  end

  def select_with_ajax
    @event = Event.find(params[:event_id])
    @users = @event.users.find(:all, :conditions => ["level = ?", params[:level]]).collect { |user|
                                unless user.conventioners.find_by_event_id(@event.id).room
                                  [user.name, user.id]
                                end
                              }
    render(:layout => false)
  end

  def add_user
    @room = Room.find(params[:id])
    @user= User.find(params[:user][:name])
    conventioner = @user.conventioners.find_by_event_id(@room.event_id)
    conventioner.room = @room
#    if @room.users << @user
    if conventioner.save
      render :update do |page|
        page.insert_html(:bottom,"room_#{@room.id}_users",:partial=>'room_user',:locals=>{:room=>@room,:user=>@user})
        page.replace_html 'unchecked_in',conventioner.event.unchecked_in.size
        page['unchecked_in'].visual_effect :highlight
        page.replace_html 'checked_in',conventioner.event.checked_in.size
        page['checked_in'].visual_effect :highlight
      end
    end
  end

private
  def del(obj)
    if obj.destroy
      render :update do |page|
        page.remove("room_#{obj.id}")
      end
    end
  end


  def query_statistics
    @event = Event.find(params[:event_id])
    @room_groups=@event.rooms.find(:all,:select=>'room_category,count(room_category) as count',:group=>'room_category')
    @room_hash={}
    @room_groups.each do |room_group|
      @room_hash[room_group.room_category]={}
      @room_hash[room_group.room_category]["allocation"]=[]
      @room_hash[room_group.room_category]["unallocation"]=[]
      rooms = @event.rooms.find(:all,:conditions=>"room_category='#{room_group.room_category}'")
      rooms.each do |room|
        if room.conventioners.size>0
          @room_hash[room_group.room_category]["allocation"] << room
        else
          @room_hash[room_group.room_category]["unallocation"] << room
        end
      end
    end
  end
end
